package org.liaohailong.victorlibrary.ui

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.activity_matrix.*
import org.liaohailong.victorlibrary.R
import org.liaohailong.victorlibrary.base.BaseActivity
import org.liaohailong.victorlibrary.widget.RedPacketEnterAnimView

/**
 * Author: liaohailong
 * Time: 2020/8/6 09:17
 * Describe: 矩阵变换操作
 */
class MatrixActivity : BaseActivity() {
    companion object {
        fun show(context: Context) {
            val intent = Intent(context, MatrixActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(MatrixTestView(this))
        setContentView(R.layout.activity_matrix)

        redRacket.init(Color.YELLOW,
                Color.RED,
                0.8f,
                0.2f,
                object : RedPacketEnterAnimView.OnAnimRunningCallback {
                    override fun onAnchorUpdate(x: Int, y: Int, progress: Float) {
                        // 平移
                        val measuredWidth = ivAnchor.measuredWidth
                        val measuredHeight = ivAnchor.measuredHeight
                        val layoutParams = ivAnchor.layoutParams as FrameLayout.LayoutParams
                        layoutParams.gravity = Gravity.START
                        layoutParams.leftMargin = x - (measuredWidth / 2)
                        layoutParams.topMargin = y - (measuredHeight / 2)
                        ivAnchor.layoutParams = layoutParams

                        // 缩小
                        val scale = (1.0f - 0.5 * progress).toFloat()
                        ivAnchor.scaleX = scale
                        ivAnchor.scaleY = scale
                    }
                })
        redRacket.setOnClickListener {
            redRacket.start(300)
        }
    }
}